// 从axios框架导入axios组件
import axios from "axios";

// 创建 axios 实例，并设置不同的 baseURL
const axiosInstance = axios.create({
    baseURL: "http://localhost:8090"  // 文件1的接口前缀
});

const contentService = {
    doGet(url, params) {
        return axiosInstance.get(url, { params });
    },
    doPost(url, data) {
        return axiosInstance.post(url, data);
    },
    doPut(url, data) {
        return axiosInstance.put(url, data);
    },
    doDelete(url) {
        return axiosInstance.delete(url);
    },
    setupInterceptors() {
        axiosInstance.interceptors.request.use(config => {
            const token = sessionStorage.getItem('token') || localStorage.getItem('token');
            if (token) {
                config.headers['Authorization'] = token;
            }
            return config;
        }, error => {
            return Promise.reject(error);
        });

        axiosInstance.interceptors.response.use(response => {
            if (response.data.code > 900) {
                alert(response.data.msg + "，是否重新去登录？");
                sessionStorage.removeItem('token');
                localStorage.removeItem('token');
                window.location.href = "/";
            }
            return response;
        }, error => {
            return Promise.reject(error);
        });
    }
};

// 默认导出 apiService 对象
export default contentService;
